package com.example.lanqiu1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.alibaba.fastjson.JSON;
import com.example.lanqiu1.websocket.JWebSocketClient;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button stopBtn;
    Button left1,left2,left3,left4,left5,left6,left7;
    Button right1,right2,right3,right4,right5,right6,right7;
    JWebSocketClient client;
    EditText mainTeamNameEdit;
    EditText passengerTeamNameEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        stopBtn = findViewById(R.id.stop_btn);
        left1 = findViewById(R.id.left1);
        left2 = findViewById(R.id.left2);
        left3 = findViewById(R.id.left3);
        left4 = findViewById(R.id.left4);
        left5 = findViewById(R.id.left5);
        left6 = findViewById(R.id.left6);
        left7 = findViewById(R.id.left7);
        right1 = findViewById(R.id.right1);
        right2 = findViewById(R.id.right2);
        right3 = findViewById(R.id.right3);
        right4 = findViewById(R.id.right4);
        right5 = findViewById(R.id.right5);
        right6 = findViewById(R.id.right6);
        right7 = findViewById(R.id.right7);
        passengerTeamNameEdit = findViewById(R.id.passengerTeamNameEdit);
        mainTeamNameEdit = findViewById(R.id.mainTeamNameEdit);
        URI uri = URI.create("ws://120.77.179.45:8083/socket/receive");
        client = new JWebSocketClient(uri) {
            @Override
            public void onMessage(String message) {
            }
        };
        client.connect();

        stopBtn.setOnClickListener(this);
        left1.setOnClickListener(this);
        left2.setOnClickListener(this);
        left3.setOnClickListener(this);
        left4.setOnClickListener(this);
        left5.setOnClickListener(this);
        left6.setOnClickListener(this);
        left7.setOnClickListener(this);
        right1.setOnClickListener(this);
        right2.setOnClickListener(this);
        right3.setOnClickListener(this);
        right4.setOnClickListener(this);
        right5.setOnClickListener(this);
        right6.setOnClickListener(this);
        right7.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        JSONObject jsonObject = new JSONObject();
        String mainTeam = mainTeamNameEdit.getText().toString();
        String passengerTeam = passengerTeamNameEdit.getText().toString();
;        switch (v.getId()){
            case R.id.stop_btn:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",0);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.left1:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",1);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.left2:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",2);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.left3:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",3);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.left4:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("passengerTeam",passengerTeam);
                    jsonObject.put("voice",4);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.left5:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",5);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.left6:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",6);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.left7:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",7);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;

            case R.id.right1:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",21);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.right2:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",22);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.right3:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",23);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.right4:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",24);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.right5:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",25);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.right6:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",26);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.right7:
                try {
                    jsonObject.put("mainTeam",mainTeam);
                    jsonObject.put("voice",27);
                    jsonObject.put("passengerTeam",passengerTeam);
                    client.send(jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;

        }
    }
}